<!DOCTYPE html>












  


<html class="theme-next pisces use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=7.1.2">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.1.2">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.1.2">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.1.2">


  <link rel="mask-icon" href="/images/logo.svg?v=7.1.2" color="#222">







<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '7.1.2',
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    back2top: true,
    back2top_sidebar: false,
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="keywords" content="iOS">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS13和Xcode11踩坑">
<meta property="og:url" content="https://rencheng.cc/2019/07/24/ios/general/iOS13和Xcode11踩坑/index.html">
<meta property="og:site_name" content="PHOENIX&#39;s Blog">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://object.renchengqi.com/blog/g8big.png">
<meta property="og:image" content="https://object.renchengqi.com/blog/ojqu0.gif">
<meta property="og:image" content="https://object.renchengqi.com/blog/mefgj.png">
<meta property="og:image" content="https://object.renchengqi.com/blog/m2g0d.gif">
<meta property="og:image" content="https://object.renchengqi.com/blog/kpbaf.jpg">
<meta property="og:image" content="https://object.renchengqi.com/blog/i0378.jpg">
<meta property="og:updated_time" content="2019-07-24T06:41:04.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="iOS13和Xcode11踩坑">
<meta name="twitter:image" content="https://object.renchengqi.com/blog/g8big.png">



  <link rel="alternate" href="/atom.xml" title="PHOENIX's Blog" type="application/atom+xml">



  
  
  <link rel="canonical" href="https://rencheng.cc/2019/07/24/ios/general/iOS13和Xcode11踩坑/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>iOS13和Xcode11踩坑 | PHOENIX's Blog</title>
  












  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">PHOENIX's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <h1 class="site-subtitle" itemprop="description">Enjoy the journey of life</h1>
      
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-about">

    
    
    
      
    

    

    <a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i> <br>关于</a>

  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
        </li>
      
    </ul>
  

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://rencheng.cc/2019/07/24/ios/general/iOS13和Xcode11踩坑/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="任成">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.JPG">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="PHOENIX's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">iOS13和Xcode11踩坑

              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-07-24 14:41:04" itemprop="dateCreated datePublished" datetime="2019-07-24T14:41:04+08:00">2019-07-24</time>
            

            
          </span>

          

          
            
            
          

          
          

          

          
            <div class="post-symbolscount">
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                <span title="本文字数">6k</span>
              

              

              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><img src="https://object.renchengqi.com/blog/g8big.png" alt></p>
<a id="more"></a>
<p>今年整体上问题不大，没有出现编译报错问题。</p>
<p>用 Xcode10 编译的 App 在 iOS 13 上使用甚至几乎完美<br>用 Xcode11 编译的 App 再在 iOS 13 上跑，就有些问题了</p>
<h2 id="modal新样式"><a href="#modal新样式" class="headerlink" title="modal新样式"></a>modal新样式</h2><p>iOS 13 多了一个新的枚举类型 <code>UIModalPresentationAutomatic</code>，且是<code>modalPresentationStyle</code>的默认值。</p>
<p><code>UIModalPresentationAutomatic</code>实际是表现是在 =iOS 13的设备上被映射成<code>UIModalPresentationPageSheet</code>。</p>
<p>我这边的设计师表示，新样式不错，可以不用改😆。</p>
<p>不过， <code>PageSheet</code> 与 <code>FullScreen</code> 对比 有个需要注意的地方，控制器的生命周期有点区别：</p>
<p>以 <code>控制器A</code>、<code>控制器B</code> 举例：</p>
<ul>
<li><p><code>控制器A</code> present <code>控制器B</code><br>  <code>控制器A</code> 不会调用 <code>viewWillDisappear</code> 以及 <code>viewDidDisappear</code></p>
</li>
<li><p><code>控制器B</code> dismiss 时<br>  <code>控制器A</code> 不会调用 <code>viewWillAppear</code> 以及 <code>viewDidAppear</code></p>
</li>
</ul>
<p>那么如果有些业务逻辑会在<code>控制器A</code>的生命周期里做的话，就需要考虑其他方式实现，或者改回<code>UIModalPresentationFullScreen</code><br><img src="https://object.renchengqi.com/blog/ojqu0.gif" alt></p>
<p>如果需要改成原本全屏的样式，可以处理Controller:</p>
<ul>
<li>初始化时设置<code>modalPresentationStyle</code>值</li>
<li>跳转修改<code>modalPresentationStyle</code>值</li>
<li>覆盖<code>modalPresentationStyle</code>的get方法</li>
</ul>
<p>看上面gif，用户是可以通过手势下拉关闭被present出来的控制器的，那如果我需要禁止他下来要怎么实现呢？</p>
<blockquote>
<p>可以参考<span class="exturl" data-url="aHR0cHM6Ly9kZXZlbG9wZXIuYXBwbGUuY29tL2RvY3VtZW50YXRpb24vdWlraXQvdmlld19jb250cm9sbGVycy9kaXNhYmxpbmdfcHVsbGluZ19kb3duX2Ffc2hlZXQ/bGFuZ3VhZ2U9b2JqYw==" title="https://developer.apple.com/documentation/uikit/view_controllers/disabling_pulling_down_a_sheet?language=objc">disabling_pulling_down_a_sheet<i class="fa fa-external-link"></i></span>的Demo</p>
</blockquote>
<p>设置<code>presentationController.delegate</code> 代理对象，实现<code>UIAdaptivePresentationControllerDelegate</code>协议方法<br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">@interface</span> <span class="title">XXViewController</span> () &lt;<span class="title">UIAdaptivePresentationControllerDelegate</span>&gt;</span></span><br><span class="line"><span class="keyword">@property</span> (<span class="keyword">nonatomic</span>, <span class="keyword">assign</span>) <span class="built_in">BOOL</span> allowBack;</span><br><span class="line"><span class="keyword">@end</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">@implementation</span> <span class="title">XXViewController</span></span></span><br><span class="line">- (<span class="keyword">void</span>)viewDidLoad &#123;</span><br><span class="line">    [<span class="keyword">super</span> viewDidLoad];</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 如果vc被navigation套了一层就要取navigation的presentationController</span></span><br><span class="line">    <span class="keyword">self</span>.navigationController.presentationController.delegate = <span class="keyword">self</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 使用UIAdaptivePresentationControllerDelegate 协议方法控制能否下拉dismiss</span></span><br><span class="line"><span class="comment">// 或者不实现这个协议，设置self.modalInPresentation（YES-不允许下拉关闭，NO-可以下拉关闭）</span></span><br><span class="line">- (<span class="built_in">BOOL</span>)presentationControllerShouldDismiss:(<span class="built_in">UIPresentationController</span> *)presentationController &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">self</span>.allowBack;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果 self.modalInPresentation=YES 或者</span></span><br><span class="line"><span class="comment">// 上面协议方法return NO，用户再下拉就会在这收到回调</span></span><br><span class="line">- (<span class="keyword">void</span>)presentationControllerDidAttemptToDismiss:(<span class="built_in">UIPresentationController</span> *)presentationController &#123;</span><br><span class="line">    <span class="comment">// 如果self.allowBack == NO，会回调到这里</span></span><br><span class="line">    <span class="comment">// 可以在这里做一些事情然后再考虑让 self.allowBack=YES 或</span></span><br><span class="line">    <span class="comment">// 则让 self.modalInPresentation=NO 即可让页面可以下拉返回</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">@end</span></span><br></pre></td></tr></table></figure></p>
<h2 id="textfield-leftview"><a href="#textfield-leftview" class="headerlink" title="textfield.leftview"></a>textfield.leftview</h2><p>如下方式，直接给<code>textfield.leftView</code>赋值一个<code>UILabel</code>对象，他的宽高会被 <code>sizeToFit</code>，而不是创建时的值。<br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// left view label</span></span><br><span class="line"><span class="built_in">UILabel</span> *phoneLabel = [[<span class="built_in">UILabel</span> alloc] initWithFrame:<span class="built_in">CGRectMake</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">63</span>, <span class="number">50</span>)];</span><br><span class="line">phoneLabel.text = <span class="string">@"手机号"</span>;</span><br><span class="line">phoneLabel.font = [<span class="built_in">UIFont</span> systemFontOfSize:<span class="number">16</span>];</span><br><span class="line"><span class="comment">// set textfield left view</span></span><br><span class="line"><span class="keyword">self</span>.textfieldName.leftView = phoneLabel;</span><br></pre></td></tr></table></figure></p>
<p>如所看到，实际leftview的width为59，height为19：<br><img src="https://object.renchengqi.com/blog/mefgj.png" alt><br>通过监听<code>leftView</code>的<code>frame</code>变化，发现是<code>layoutSubview</code>之后变化的。<br>最终还是给UILabel多套了一个UIView来解决<br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// label</span></span><br><span class="line"><span class="built_in">UILabel</span> *phoneLabel = [[<span class="built_in">UILabel</span> alloc] init];</span><br><span class="line">phoneLabel.text = <span class="string">@"手机号"</span>;</span><br><span class="line">phoneLabel.font = [<span class="built_in">UIFont</span> systemFontOfSize:<span class="number">16</span>];</span><br><span class="line">[phoneLabel sizeToFit];</span><br><span class="line">phoneLabel.centerY = <span class="number">50</span>/<span class="number">2.</span>f;</span><br><span class="line"><span class="comment">// left view</span></span><br><span class="line"><span class="built_in">UIView</span> *leftView = [[<span class="built_in">UIView</span> alloc] initWithFrame:(<span class="built_in">CGRect</span>)&#123;<span class="number">0</span>, <span class="number">0</span>, <span class="number">63</span>, <span class="number">50</span>&#125;];</span><br><span class="line">[leftView addSubview:phoneLabel];</span><br><span class="line"><span class="comment">// set textfield left view</span></span><br><span class="line"><span class="keyword">self</span>.textfieldName.leftView = leftView;</span><br></pre></td></tr></table></figure></p>
<h2 id="KVC访问私有属性Crash"><a href="#KVC访问私有属性Crash" class="headerlink" title="KVC访问私有属性Crash"></a>KVC访问私有属性Crash</h2><p>打开有<code>UISearchBar</code>的页面发现Crash了，看到控制台输出提示：<br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 获取_searchField</span></span><br><span class="line"><span class="built_in">UITextField</span> *sField = [<span class="keyword">self</span>.searchbar valueForKey:<span class="string">@"_searchField"</span>];</span><br><span class="line"></span><br><span class="line"><span class="comment">// crash log</span></span><br><span class="line">*** Terminating app due to uncaught exception 'NSGenericException', reason: </span><br><span class="line">'Access to UISearchBar's _searchField ivar is prohibited. </span><br><span class="line">This is an application bug<span class="string">'</span></span><br></pre></td></tr></table></figure></p>
<p><strong>看起来是禁止访问私有属性了。</strong></p>
<p>用 Xcode 10 编译的 App 在 iOS 13 上能正常使用，那么就是 Xcode 11 做了限制访问私有属性的一些处理了。</p>
<p>偶然发现 iOS 13 中增加了<code>UISearchTextField</code>类，且暴露了<code>searchTextField</code>。<br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// UISearchTextField.h</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">UIKIT_CLASS_AVAILABLE_IOS_ONLY</span>(<span class="number">13.0</span>)</span><br><span class="line"><span class="class"><span class="keyword">@interface</span> <span class="title">UISearchTextField</span> : <span class="title">UITextField</span></span></span><br><span class="line"><span class="comment">// ...</span></span><br><span class="line"><span class="keyword">@end</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">@interface</span> <span class="title">UISearchBar</span> (<span class="title">UITokenSearch</span>)</span></span><br><span class="line"><span class="keyword">@property</span> (<span class="keyword">nonatomic</span>, <span class="keyword">readonly</span>) <span class="built_in">UISearchTextField</span> *searchTextField;</span><br><span class="line"><span class="keyword">@end</span></span><br></pre></td></tr></table></figure></p>
<p>但是仅在 iOS 13 以上系统支持，还是暂时用遍历view的方式去做了😂</p>
<h2 id="navigationBar-shouldPopItem"><a href="#navigationBar-shouldPopItem" class="headerlink" title="navigationBar:shouldPopItem:"></a>navigationBar:shouldPopItem:</h2><p>点击导航栏返回的时候Crash了，控制台输出提示：<br><figure class="highlight ada"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Теrmіnаtіng арр due to uncaught <span class="keyword">exception</span>' NSInternalInconsistencyException' , </span><br><span class="line">reason : ' <span class="type">Override</span> <span class="keyword">of</span> -navigationBar : <span class="type">shouldPopItem</span>: returned YES after </span><br><span class="line">manually popping a view controller ( navigat ionController=&lt;MHCRNavgationController : 0<span class="type">x106039400</span>&gt;) '</span><br></pre></td></tr></table></figure></p>
<p>因为我们工程里，基本上所有的<code>Controller</code>是继承基类<code>BaseViewController</code>并实现<code>- (BOOL)naviBack:</code>方法，用于实现在用户点击返回和侧滑返回时，一些不能返回的特殊处理。<br>其根本原理是通过实现<code>UINavgationBar</code>的代理方法<code>- (BOOL)navigationBar:shouldPopItem:</code>来做的控制：<br><figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="built_in">BOOL</span>)navigationBar:(<span class="built_in">UINavigationBar</span> *)navigationBar</span><br><span class="line">        shouldPopItem:(<span class="built_in">UINavigationItem</span> *)item &#123;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 默认可以返回</span></span><br><span class="line">    <span class="built_in">BOOL</span> canGoBack = <span class="literal">YES</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// BaseViewController定义协议方法判断能否能点击返回上一层</span></span><br><span class="line">    <span class="built_in">UIViewController</span> *vc = <span class="keyword">self</span>.viewControllers.lastObject;</span><br><span class="line">    <span class="keyword">if</span> ([vc isKindOfClass:BaseViewController.class]) &#123;</span><br><span class="line">        canGoBack = [(BaseViewController *)vc naviBack:<span class="literal">nil</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span> (canGoBack) &#123;</span><br><span class="line">        [<span class="keyword">self</span> popViewControllerAnimated:<span class="literal">YES</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> canGoBack;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>但是我实现的时候有<code>Return YES</code>啊！想了想，试着<strong>注释</strong>了<code>[self popViewControllerAnimated:YES]</code>，发现没有崩溃了。<br>但是在iOS 12上，会发现控制器没有回到上一层，如图，只有navbar回到上一层了：<br><img src="https://object.renchengqi.com/blog/m2g0d.gif" alt><br>好吧，那只能判断一下版本解决这个问题了，修改方式：<br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="built_in">BOOL</span>)navigationBar:(<span class="built_in">UINavigationBar</span> *)navigationBar</span><br><span class="line">        shouldPopItem:(<span class="built_in">UINavigationItem</span> *)item &#123;</span><br><span class="line">        </span><br><span class="line">    <span class="comment">// 判断 iOS 版本低于13</span></span><br><span class="line">    <span class="built_in">BOOL</span> bellow13 = !@available(iOS <span class="number">13.0</span>, *);</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 默认可以返回</span></span><br><span class="line">    <span class="built_in">BOOL</span> canGoBack = <span class="literal">YES</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// BaseViewController定义协议方法判断能否能点击返回上一层</span></span><br><span class="line">    <span class="built_in">UIViewController</span> *vc = <span class="keyword">self</span>.viewControllers.lastObject;</span><br><span class="line">    <span class="keyword">if</span> ([vc isKindOfClass:BaseViewController.class]) &#123;</span><br><span class="line">        canGoBack = [(BaseViewController *)vc naviBack:<span class="literal">nil</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span> (canGoBack &amp;&amp; bellow13) &#123;</span><br><span class="line">        <span class="comment">// 如果低于13且可以返回，就执行popViewController</span></span><br><span class="line">        [<span class="keyword">self</span> popViewControllerAnimated:<span class="literal">YES</span>];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> canGoBack;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="夜间模式"><a href="#夜间模式" class="headerlink" title="夜间模式"></a>夜间模式</h2><p>WWDC 19 直播的时候看到夜间模式，老实说挺开心的，直到我用 Xcode 11 开始做适配，妈耶！x N</p>
<p><strong>注意：使用 Xcode 10 编译的 App 依然是日间模式，不会产生效果！！！</strong></p>
<p>初步扫了一下出现的问题如下图，大致情况是：没有设置背景色的系统控件会被设置成黑色，部分控件是tintColor没设置的话也会被改。<br><img src="https://object.renchengqi.com/blog/kpbaf.jpg" alt><br><img src="https://object.renchengqi.com/blog/i0378.jpg" alt></p>
<p>由于Assets里的Color配置是<code>iOS 12</code>以上才能使用的，所以如果没有做全局主题色设计且需要支持iOS 12以下设备，改起来会比较恶心。</p>
<p>对此现象，找设计师沟通。设计师表示，暂时没有精力做夜间模式规划。<br>设计师问：能否强制只日间模式？<br>答：能。配置方式有两种，<code>单页面配置</code> 和 <code>全局配置</code>。</p>
<ul>
<li>单页配置<br>  将需要配置的<code>UIViewControler</code>对象的<code>overrideUserInterfaceStyle</code>属性设置成<code>UIUserInterfaceStyleLight</code>或者<code>UIUserInterfaceStyleDark</code>以强制是某个页面显示为<code>浅/深色模式</code></li>
<li>全局配置<br>  在工程的<code>Info.plist</code>的中，增加/修改<code>UIUserInterfaceStyle</code>为<code>UIUserInterfaceStyleLight</code>或<code>UIUserInterfaceStyleDark</code></li>
</ul>
<h2 id="禁用方法"><a href="#禁用方法" class="headerlink" title="禁用方法"></a>禁用方法</h2><p><strong>textField 更改holder颜色崩溃</strong><br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[textField setValue:HexColor(<span class="number">0x999999</span>) forKeyPath:<span class="string">@"_placeholderLabel.textColor"</span>];</span><br></pre></td></tr></table></figure></p>
<p><strong>解决方式</strong><br><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">NSAttributedString</span> *attrString = [[<span class="built_in">NSAttributedString</span> alloc] initWithString:<span class="string">@"请输入占位文字"</span> attributes:@&#123;<span class="built_in">NSForegroundColorAttributeName</span>:HexColor(<span class="number">0x999999</span>), <span class="built_in">NSFontAttributeName</span>:textField.font&#125;];</span><br><span class="line">textField.attributedPlaceholder = attrString;</span><br></pre></td></tr></table></figure></p>

      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/iOS/" rel="tag"># iOS</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/07/20/ios/swift/基础语法/" rel="next" title="Swift">
                <i class="fa fa-chevron-left"></i> Swift
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/07/30/ios/general/DarkMode/" rel="prev" title="iOS-支持Dark Mode">
                iOS-支持Dark Mode <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.JPG" alt="任成">
            
              <p class="site-author-name" itemprop="name">任成</p>
              <div class="site-description motion-element" itemprop="description"></div>
          </div>

          

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL0NvZGVyQ3VydmU=" title="GitHub &rarr; https://github.com/CoderCurve"><i class="fa fa-fw fa-github"></i>GitHub</span>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <span class="exturl" data-url="bWFpbHRvOnJlbmNoZW5nMTFAaWNsb3VkLmNvbQ==" title="E-Mail &rarr; mailto:rencheng11@icloud.com"><i class="fa fa-fw fa-envelope"></i>E-Mail</span>
                </span>
              
            </div>
          

          

          
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#modal新样式"><span class="nav-number">1.</span> <span class="nav-text">modal新样式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#textfield-leftview"><span class="nav-number">2.</span> <span class="nav-text">textfield.leftview</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#KVC访问私有属性Crash"><span class="nav-number">3.</span> <span class="nav-text">KVC访问私有属性Crash</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#navigationBar-shouldPopItem"><span class="nav-number">4.</span> <span class="nav-text">navigationBar:shouldPopItem:</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#夜间模式"><span class="nav-number">5.</span> <span class="nav-text">夜间模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#禁用方法"><span class="nav-number">6.</span> <span class="nav-text">禁用方法</span></a></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>
  


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">  <span class="exturl" data-url="aHR0cDovL3d3dy5iZWlhbi5taWl0Lmdvdi5jbg==">京ICP备18001214号 </span>&copy; 2014 – <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">任成</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
    <span title="站点总字数">277k</span>
  

  
</div>









        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  <script src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/utils.js?v=7.1.2"></script>

  <script src="/js/motion.js?v=7.1.2"></script>



  
  


  <script src="/js/affix.js?v=7.1.2"></script>

  <script src="/js/schemes/pisces.js?v=7.1.2"></script>



  
  <script src="/js/scrollspy.js?v=7.1.2"></script>
<script src="/js/post-details.js?v=7.1.2"></script>



  


  <script src="/js/next-boot.js?v=7.1.2"></script>


  

  
  <script src="/js/exturl.js?v=7.1.2"></script>


  

  


  


  
  <script>
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url).replace(/\/{2,}/g, '/');
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x"></i></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x"></i></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  
  

  
  

  


  

  
<script>
if ($('body').find('pre.mermaid').length) {
  $.ajax({
    type: 'GET',
    url: '//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js',
    dataType: 'script',
    cache: true,
    success: function() {
      mermaid.initialize({
        theme: 'forest',
        logLevel: 3,
        flowchart: { curve: 'linear' },
        gantt: { axisFormat: '%m/%d/%Y' },
        sequence: { actorMargin: 50 }
      });
    }
  });
}
</script>


  

  

  

  

  

  

  

  

  

</body>
</html>
